package cn.com.hiocde.myresume.fragment;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.felipecsl.gifimageview.library.GifImageView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import cn.com.hiocde.myresume.R;
import cn.com.hiocde.myresume.activity.ScaleImage;

/**
 * Created by Hmqi on 2016/5/31.
 */
public class BaseInfoFragment extends Fragment {

    private View view=null;
    private MediaPlayer mediaPlayer=null;
    private GifImageView gif_view=null;
    private boolean playing=false;

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {

        view= inflater.inflate(R.layout.fragment_home_view0, container, false);

        ImageView img_view=(ImageView)view.findViewById(R.id.photo);//null pointer(view==null)

        img_view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(getActivity(), ScaleImage.class);
                intent.putExtra("img_id",R.mipmap.me_true);
                startActivity(intent);
            }
        });

        initGifView();
        gif_view.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                if(playing){
                    gif_view.stopAnimation();
                    mediaPlayer.reset();
                    playing=false;
                }else{
                    gif_view.startAnimation();//start gif
                    initMediaPlayer();
                    mediaPlayer.start();//start playing
                    playing=true;
                }
            }
        });

        return view;
    }

    private void initGifView(){
        gif_view=(GifImageView)view.findViewById(R.id.gifImageView);
        InputStream is = getContext().getResources().openRawResource(R.raw.horn);
        byte[] array = streamToBytes(is);
        gif_view.setBytes(array);
    }

    private void initMediaPlayer(){
        mediaPlayer=MediaPlayer.create(getActivity(), R.raw.my_introduction);
//        try {
//            mediaPlayer.prepare();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
    }

    private static byte[] streamToBytes(InputStream is) {
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = is.read(buffer)) >= 0) {
                os.write(buffer, 0, len);
            }
        } catch (java.io.IOException e) {
        }
        return os.toByteArray();
    }

    public View getView(){
        return view;
    }

}
